package kz.edu.sdu.atm;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.math.BigDecimal;

import kz.edu.sdu.atm.db.FileDbAdapter;
import kz.edu.sdu.atm.db.IGenericDbAdapter;
import kz.edu.sdu.atm.models.UserDTO;
import kz.edu.sdu.atm.util.Logger;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestFileDbAdapter {

	public Logger log = new Logger(TestFileDbAdapter.class);
	IGenericDbAdapter adapter;
	FileDbAdapter fileAdapter;

	@Before
	public void setUp() throws Exception {
		fileAdapter = new FileDbAdapter(new File("store.txt"));
		adapter = fileAdapter;
	}

	@After
	public void tearDown() throws Exception {
		fileAdapter.flush();
	}
	
	public String cat(File file) throws Exception{
		BufferedReader in = new BufferedReader(new FileReader(file));
		StringBuffer b = new StringBuffer();
		while (in.ready()){
			b.append(String.format("%s\n", in.readLine()));
		}
		in.close();
		return b.toString();
	}
	
	@Test
	public void testCreateUser() throws Exception {
		UserDTO u = new UserDTO();
		u.setUsername("german");
		u.setPassword("q");
		u.setMoney(BigDecimal.ZERO);
		adapter.saveUser(u);
		fileAdapter.flush();
		log.notice(cat(new File("store.txt")));
	}

	@Test
	public void testGetUser() throws Exception {
		UserDTO u = adapter.getUser("german");
		log.debug(String.format("%s, %s, %f", u.getUsername(), u.getPassword(),
				u.getMoney()));
	}

}
